<template>
  <div class="warp_auto">
    <div class="warp_top">
      <div class="jiansuo-box">
        <span class="span_top">模板名称：</span>
        <el-input
          v-model="templateName"
          class="input"
          placeholder="请输入模板名称"
        />
      </div>

      <div class="jiansuo-box">
        <span class="span_statr1">模板状态：</span>
        <el-select
          v-model="templateStatus"
          placeholder="请选择状态"
          clearable
          class="input"
        >
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </div>

      <div class="jiansuo-box">
        <el-button
          type="primary"
          icon="el-icon-search"
          class="btn"
          @click="btn"
        >检索
        </el-button>
        <el-button
          type="primary"
          class="btn"
          @click="addTemplate"
        >新增模板
        </el-button>
      </div>
    </div>

    <!--模板（表格）-->
    <el-card>
      <el-table
        :data="templateArr"
        style="width: 100%"
        height="540"
        stripe
        :header-cell-style="{ background: '#d2e0f9', color: '#606266' }"
      >
        <el-table-column
          type="index"
          width="90"
          label="序号"
          align="center"
        />
        <el-table-column label="模板名称" min-width="20%" align="center">
          <template slot-scope="scope">
              <span style="margin-left: 10px">{{
                  scope.row.template_item_name
                }}</span>
          </template>
        </el-table-column>
        <el-table-column label="简称" min-width="12%" align="center">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.spell }}</span>
          </template>
        </el-table-column>
        <el-table-column label="类型" min-width="15%" align="center">
          <span>{{this.templateTypeName}}</span>
        </el-table-column>
        <el-table-column
          property="status"
          width="180"
          align="center"
          label="状态"
          header-align="center"
        >
          <template scope="scope">
            <el-switch
              v-model="scope.row.status"
              class="switch-btn"
              inactive-text="禁用"
              active-text="启用"
              :active-value="1"
              :inactive-value="0"
              active-color="#02538C"
              inactive-color="#B9B9B9"
              @change="changeSwitchs(scope.$index, scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" min-width="10%">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.sort_num }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建人工号" align="center" min-width="10%">
          <template slot-scope="scope">
            <span
              style="margin-left: 10px">{{
                scope.row.operator_code == 'admin' ? '管理员' : scope.row.operator_code
              }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否公开" align="center" min-width="10%">
          <template slot-scope="scope">
            <span
              style="margin-left: 10px">{{
                scope.row.public_flag === 1 ? "是" : scope.row.public_flag === 2 ? "否" : ""
              }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" min-width="18%">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)"
            >编辑
            </el-button>
            <el-popover
              :ref="`popover-${scope.$index}`"
              trigger="click"
              placement="top"
              width="100"
            >
              <p class="el-icon-warning">确定删除</p>
              <div style="text-align: right; margin: 0">
                <el-button
                  size="mini"
                  type="text"
                  @click="templateDeleteCancel(scope)"
                >取消
                </el-button>
                <el-button
                  type="primary"
                  size="mini"
                  @click="templateDeleteSure(scope)"
                >确定
                </el-button>
              </div>
              <el-button
                slot="reference"
                size="mini"
                style="margin-left: 10px"
                type="danger"
              >删除
              </el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="paging">
        <el-pagination
          :current-page="start"
          :page-size="PageSize"
          :pager-count="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!--护理模板编辑-->
    <el-dialog
      title="编辑护理模板"
      class="mod_box"
      :visible.sync="templateEditDialogVisble"
      :show-close="false"
    >
      <el-form class="form" label-width="80px">
        <el-form-item label="模板名称 : ">
          <el-input
            v-model="dialogInfo.template_item_name"
            class="bianji"
          />
        </el-form-item>
        <el-form-item label="简拼 : ">
          <el-input v-model="dialogInfo.spell"/>
        </el-form-item>
        <el-form-item label="排序 : ">
          <el-input v-model="dialogInfo.sort_num" />
        </el-form-item>
        <el-form-item label="类型 : ">
          <el-input :value="templateTypeName" disabled />
        </el-form-item>
        <el-form-item label="状态 : ">
          <el-switch
            v-model="dialogInfo.status"
            class="switch-btn"
            inactive-text="禁用"
            active-text="启用"
            :active-value="1"
            :inactive-value="0"
            active-color="#02538C"
            inactive-color="#B9B9B9"
          />
        </el-form-item>
        <el-form-item label="内容 : ">
          <el-input
            v-model="dialogInfo.content"
            type="textarea"
            :rows="3"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="是否公开">
          <el-select
            v-model="dialogInfo.public_flag"
            placeholder="请选择"
          >
            <el-option
              v-for="item in publicList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="templateEditDialogVisble = false">取 消</el-button>
        <el-button type="primary" @click="templateEditSaveClick">保 存</el-button>
      </div>
    </el-dialog>

    <!-- 新增模板 -->
    <el-dialog
      title="新增护理模板"
      class="mod_box"
      :visible.sync="dialogFormVisible"
      :show-close="false"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="模板名称 : " prop="templateItemName">
          <el-input
            v-model="ruleForm.templateItemName"
            placeholder="请输入模板名称"
            class="el-inputer"
          />
        </el-form-item>
        <el-form-item label="简拼 : " prop="spell">
          <el-input
            v-model="ruleForm.spell"
            placeholder="请输入简拼"
            class="el-inputer"
          />
        </el-form-item>
        <el-form-item label="类型 : " prop="templateItemName">
          <el-input
            :value="this.templateTypeName"
            disabled
            class="el-inputer"
          />
        </el-form-item>
        <el-form-item label="状态 : " prop="status">
          <el-switch
            v-model="ruleForm.status"
            class="switch-btn"
            inactive-text="禁用"
            active-text="启用"
            :active-value="1"
            :inactive-value="0"
            active-color="#02538C"
            inactive-color="#B9B9B9"
          />
        </el-form-item>
        <el-form-item label="排序 : " prop="sortNum">
          <el-input
            v-model="ruleForm.sortNum"
            class="el-inputer"
          />
        </el-form-item>
        <el-form-item label="内容 : " prop="content">
          <el-input
            v-model="ruleForm.content"
            type="textarea"
            :rows="3"
            class="el-inputer"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="是否公开" prop="public_flag">
          <el-select
            v-model="ruleForm.public_flag"
            placeholder="请选择"
            class="bianji"
          >
            <el-option
              v-for="item in publicList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

      </el-form>

      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
        >保 存
        </el-button>
      </div>
    </el-dialog>

    <!-- switch-model -->
    <el-dialog title="状态更新" :visible.sync="switchUpdate" :show-close="false" class="mod_box">
      <span>
        <i class="el-icon-warning"/>此操作将会改变开关状态, 是否继续?
      </span>
      <span slot="footer">
        <el-button
          @click="(switchUpdate = false), cancelSwitch()"
        >取 消</el-button>
        <el-button
          type="primary"
          @click="(switchUpdate = false), updataSwitch()"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  Dictionaryupdate,
  getListlei,
  Dictionaryinsert,
  Dictionarydelete,
  getUser, Dictionaryedit
} from '@/api/table'

export default {
  data() {
    return {
      switchUpdate: false, // 开关-模板
      switchId: '',
      switchStatus: '',
      // 控制弹窗 显示
      templateEditDialogVisble: false,
      dialogVisibless: false,
      dialogInfo: {},
      templateName: '',
      templateStatus: '',
      templateTypeId: '',
      templateTypeName: '',  //模板类型名称
      dialogFormVisible: false,
      del: false,

      ruleForm: {
        templateId: '',
        templateItemName: '',
        spell: '',
        content: '',
        status: 1,
        sortNum: '',
        public_flag: 1,
        especially_identifying:"1"
      },
      rules: {
        templateItemName: [
          {required: true, message: '请输入模板名称', trigger: 'blur'}
        ],
        sortNum: [{required: true, message: '请输入排序', trigger: 'blur'}],
        dicTemplateName: [
          {required: true, message: '请输入模板类型名称', trigger: 'blur'}
        ],
        templateId: [
          {required: true, message: '请选择模板类型', trigger: 'change'}
        ]
      },
      options: [
        {
          id: '1',
          value: 1,
          label: '启用'
        },
        {
          id: '0',
          value: 0,
          label: '禁用'
        }
      ],
      templateArr: [],
      start: 1, // 默认显示第几页
      totalCount: 0, // 总条数，根据接口获取数据长度(注意 : 这里不能为空)
      totalCounts: 0,
      pgeSizes: [10, 20, 30, 40], // 个数选择器（可修改）
      PageSize: 10, // 默认每页显示的条数（可修改）
      dialogVisible: false, // 控制弹窗 显示
      operatorCode: "",
      //是否公开
      publicList: [
        {
          id: "0",
          value: 2,
          label: "否",
        },
        {
          id: "1",
          value: 1,
          label: "是",
        },
      ]
      // 点击查看按钮  这条数据详细信息
    }
  },
  created() {
    this.templateTypeId = this.$route.query.templateTypeId;
    this.templateTypeName = this.$route.query.templateTypeName;
    getUser().then((res) => {
      this.operatorCode = res.data.workNumber;
      this.queryTemplateList(this.PageSize, this.start);
    });
  },
  methods: {
    //模板检索
    queryTemplateList(pageSize, start) {
      getListlei({
        size: pageSize,
        start: start,
        templateItemName: this.templateName,
        templateId: this.templateTypeId,
        status: this.templateStatus === '' ? null : this.templateStatus,
        operatorCode: this.operatorCode
      }).then((res) => {
        this.templateArr = res.data;
        this.totalCount = res.totalCount;
      })
    },
    addTemplate() {
      this.ruleForm = {
        templateId: '',
        templateItemName: '',
        spell: '',
        content: '',
        status: 1,
        sortNum: '',
        public_flag: 1,
        especially_identifying:"1"
      };
      this.dialogFormVisible = true;
    },
    // 新增模板
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          Dictionaryinsert({
            templateId: this.templateTypeId,
            templateItemName: this.ruleForm.templateItemName,
            spell: this.ruleForm.spell,
            content: this.ruleForm.content,
            status: this.ruleForm.status,
            sortNum: this.ruleForm.sortNum,
            publicFlag: this.ruleForm.public_flag,
            especiallyIdentifying:this.ruleForm.especially_identifying
          }).then((res) => {
            if (res.code === 200) {
              this.$message({message: "添加成功", type: "success"});
              this.dialogFormVisible = false;
              //清空数据
              this.resetForm(ruleForm);
              this.queryTemplateList(this.PageSize, this.start);
            } else {
              this.$message({message: "添加失败", type: "warning"});
            }

          })
        } else {
          this.$message({
            message: '请填写必填项！',
            type: 'warning',
            duration: 1500
          })
          this.dialogFormVisible = true
          return false
        }
      })
    },
    //删除模板
    templateDeleteSure(scope) {
      Dictionarydelete({id: scope.row.id}).then((res) => {
        this.$message({message: '删除成功', type: 'success'});
        this.queryTemplateList(this.PageSize, this.start)
      })

      scope._self.$refs[`popover-${scope.$index}`].doClose();
    },
    templateDeleteCancel(scope) {
      this.$message('取消删除')
      scope._self.$refs[`popover-${scope.$index}`].doClose();
    },

    // 列表开关的id
    changeSwitchs(index, row) {
      this.switchId = row.id
      this.switchStatus = row.status
      this.switchUpdate = true
    },

    // 保存开关状态-模板-确认
    updataSwitch() {
      Dictionaryupdate({
        id: this.switchId,
        status: this.switchStatus
      }).then((res) => {
        this.queryTemplateList(this.PageSize, this.start)
      })
    },
    // 保存开关状态-模板-取消
    cancelSwitch() {
      this.queryTemplateList(this.PageSize, this.start) // 重新获取表格
    },

    // 搜索按钮
    btn() {
      this.start = 1;
      this.queryTemplateList(this.PageSize, this.start)
    },
    // 每页显示的条数
    handleSizeChange(val) {
      // 注意 : 在改变每页显示的条数时，要将页码显示到第一页
      this.start = 1;
      // 改变每页显示的条数
      this.PageSize = val;

      this.queryTemplateList(this.PageSize, this.start); // 模板
    },
    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.start = val;
      //先判断开关再判断请求
      this.queryTemplateList(this.PageSize, this.start) // 模板
    },
    //打开编辑模板页面
    handleEdit(info) {
      this.templateEditDialogVisble = true;
      this.dialogInfo = info;
    },

    // 子组件传 过来的 数据   模板页面
    dialogVisiblesss(v) {
      this.dialogVisiblessss = v;
      this.queryTemplateList(this.PageSize, this.start);
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 模板编辑保存
    templateEditSaveClick() {
      Dictionaryedit({
        id: this.dialogInfo.id,
        status: this.dialogInfo.status,
        templateId: this.templateTypeId,
        templateItemName: this.dialogInfo.template_item_name,
        spell: this.dialogInfo.spell,
        content: this.dialogInfo.content,
        sortNum: this.dialogInfo.sort_num==""?"":Number(this.dialogInfo.sort_num),
        publicFlag:this.dialogInfo.public_flag,
        especiallyIdentifying: '',
      }).then((res) => {
        if (res.code === 200) {
          this.$message({message: "编辑成功", type: "success"});
          this.templateEditDialogVisble = false;
        } else {
          this.$message({message: "编辑失败", type: "warning"});
          this.templateEditDialogVisble = true;
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.mod_box {
  margin-left: 28%;
  width: 44%;
}
.el-inputer {
  width: 88%;
}

</style>
